package com.huawei.hms.support.api.hwid;

import android.accounts.Account;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
import android.text.TextUtils;
import com.huawei.hms.support.api.entity.auth.Scope;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.SCOPE;
import com.huawei.hwid.core.datatype.UserInfo;
import com.huawei.updatesdk.service.otaupdate.UpdateKey;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SignInHuaweiId implements Parcelable {
    public static final Creator<SignInHuaweiId> CREATOR = new Creator<SignInHuaweiId>() {
        public /* synthetic */ Object createFromParcel(Parcel parcel) {
            return a(parcel);
        }

        public /* synthetic */ Object[] newArray(int i) {
            return a(i);
        }

        public SignInHuaweiId a(Parcel parcel) {
            return new SignInHuaweiId(parcel);
        }

        public SignInHuaweiId[] a(int i) {
            return new SignInHuaweiId[i];
        }
    };
    private String a;
    private String b;
    private String c;
    private String d;
    private String e;
    private int f;
    private int g;
    private String h;
    private String i;
    private Set<Scope> j;
    private String k;
    private String l;
    private String m;
    private Set<Scope> n = new HashSet();
    private String o;
    private long p;
    private String q;
    private String r;

    SignInHuaweiId(String str, String str2, String str3, String str4, String str5, String str6, int i, int i2, Set<Scope> set, String str7, String str8, String str9) {
        this.b = str;
        this.a = str2;
        this.c = str3;
        this.d = str4;
        this.e = str5;
        this.h = str6;
        this.f = i;
        this.g = i2;
        this.j = set;
        this.k = str7;
        this.l = str8;
        this.i = str9;
    }

    protected SignInHuaweiId(Parcel parcel) {
        readFromParcel(parcel);
    }

    protected SignInHuaweiId() {
    }

    public static SignInHuaweiId createDefault() {
        return build(null, null, null, null, null, null, 0, -1, new HashSet(), null, null, null);
    }

    public static SignInHuaweiId build(String str, String str2, String str3, String str4, String str5, String str6, int i, int i2, Set<Scope> set, String str7, String str8, String str9) {
        return new SignInHuaweiId(str, str2, str3, str4, str5, str6, i, i2, set, str7, str8, str9);
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(this.a);
        parcel.writeString(this.b);
        parcel.writeString(this.c);
        parcel.writeString(this.d);
        parcel.writeString(this.e);
        parcel.writeInt(this.f);
        parcel.writeInt(this.g);
        parcel.writeString(this.k);
        parcel.writeString(this.h);
        parcel.writeString(this.i);
        parcel.writeList(new ArrayList(this.j));
        parcel.writeString(this.l);
        parcel.writeString(this.m);
        parcel.writeString(this.o);
        parcel.writeLong(this.p);
        parcel.writeString(this.q);
        parcel.writeString(this.r);
    }

    public void readFromParcel(Parcel parcel) {
        this.a = parcel.readString();
        this.b = parcel.readString();
        this.c = parcel.readString();
        this.d = parcel.readString();
        this.e = parcel.readString();
        this.f = parcel.readInt();
        this.g = parcel.readInt();
        this.k = parcel.readString();
        this.h = parcel.readString();
        this.i = parcel.readString();
        this.j = new HashSet(parcel.createTypedArrayList(Scope.CREATOR));
        this.l = parcel.readString();
        this.m = parcel.readString();
        this.o = parcel.readString();
        this.p = parcel.readLong();
        this.q = parcel.readString();
        this.r = parcel.readString();
    }

    public static SignInHuaweiId fromJson(String str) throws JSONException {
        if (!TextUtils.isEmpty(str)) {
            return fromJson(new JSONObject(str));
        }
        throw new JSONException("invalid json format");
    }

    public static SignInHuaweiId fromJson(JSONObject jSONObject) throws JSONException {
        SignInHuaweiId signInHuaweiId = new SignInHuaweiId();
        signInHuaweiId.parserJson(jSONObject);
        return signInHuaweiId;
    }

    protected void parserJson(JSONObject jSONObject) throws JSONException {
        this.a = jSONObject.optString(Oauth2AccessToken.KEY_UID, null);
        this.b = jSONObject.optString("openId", null);
        this.c = jSONObject.optString("displayName", null);
        this.d = jSONObject.optString("photoUriString", null);
        this.e = jSONObject.optString("accessToken", null);
        this.f = jSONObject.optInt(UpdateKey.STATUS, -1);
        this.g = jSONObject.optInt(UserInfo.GENDER, -1);
        this.k = jSONObject.optString("serverAuthCode", null);
        this.h = jSONObject.optString("serviceCountryCode", null);
        this.i = jSONObject.optString("countryCode", null);
        JSONArray jSONArray = jSONObject.getJSONArray("grantedScopes");
        if (jSONArray != null) {
            Set hashSet = new HashSet();
            for (int i = 0; i < jSONArray.length(); i++) {
                String optString = jSONArray.getJSONObject(i).optString("mScopeUri", null);
                if (optString != null) {
                    hashSet.add(new Scope(optString));
                }
            }
            this.j = hashSet;
        }
        this.l = jSONObject.optString("unionId", null);
        this.m = jSONObject.optString(SCOPE.SCOPE_ACCOUNT_EMAIL, null);
        this.o = jSONObject.optString("idToken", null);
        this.p = Long.parseLong(jSONObject.getString("expirationTimeSecs"));
        this.q = jSONObject.optString("givenName", null);
        this.r = jSONObject.optString("familyName", null);
    }

    public String toJson() throws JSONException {
        return objectToJson().toString();
    }

    protected JSONObject objectToJson() throws JSONException {
        JSONObject jSONObject = new JSONObject();
        if (getUid() != null) {
            jSONObject.put(Oauth2AccessToken.KEY_UID, getUid());
        }
        if (getOpenId() != null) {
            jSONObject.put("openId", getOpenId());
        }
        if (getDisplayName() != null) {
            jSONObject.put("displayName", getDisplayName());
        }
        if (getPhotoUriString() != null) {
            jSONObject.put("photoUriString", getPhotoUriString());
        }
        if (getAccessToken() != null) {
            jSONObject.put("accessToken", getAccessToken());
        }
        jSONObject.put(UpdateKey.STATUS, getStatus());
        jSONObject.put(UserInfo.GENDER, getGender());
        if (getServerAuthCode() != null) {
            jSONObject.put("serverAuthCode", getServerAuthCode());
        }
        if (getServiceCountryCode() != null) {
            jSONObject.put("serviceCountryCode", getServiceCountryCode());
        }
        if (getCountryCode() != null) {
            jSONObject.put("countryCode", getCountryCode());
        }
        if (getUnionId() != null) {
            jSONObject.put("unionId", getUnionId());
        }
        if (getEmail() != null) {
            jSONObject.put(SCOPE.SCOPE_ACCOUNT_EMAIL, getEmail());
        }
        if (getIdToken() != null) {
            jSONObject.put("idToken", getIdToken());
        }
        jSONObject.put("expirationTimeSecs", getExpirationTimeSecs());
        if (getGivenName() != null) {
            jSONObject.put("givenName", getGivenName());
        }
        if (getFamilyName() != null) {
            jSONObject.put("familyName", getFamilyName());
        }
        return jsonPutOpt(jsonPutScopes(jSONObject));
    }

    protected JSONObject jsonPutScopes(JSONObject jSONObject) throws JSONException {
        if (getGrantedScopes() != null) {
            JSONArray jSONArray = new JSONArray();
            for (Scope scope : getGrantedScopes()) {
                JSONObject jSONObject2 = new JSONObject();
                jSONObject2.putOpt("mScopeUri", scope.getScopeUri());
                jSONArray.put(jSONObject2);
            }
            jSONObject.put("grantedScopes", jSONArray);
        }
        return jSONObject;
    }

    protected JSONObject jsonPutOpt(JSONObject jSONObject) throws JSONException {
        if (getExtensionScopes() != null) {
            JSONArray jSONArray = new JSONArray();
            for (Scope scope : getExtensionScopes()) {
                JSONObject jSONObject2 = new JSONObject();
                jSONObject2.putOpt("mScopeUri", scope.getScopeUri());
                jSONArray.put(jSONObject2);
            }
            jSONObject.putOpt("extensionScopes", jSONArray);
        }
        return jSONObject;
    }

    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof SignInHuaweiId)) {
            return false;
        }
        return getGrantedScopes().equals(((SignInHuaweiId) obj).getGrantedScopes());
    }

    public int hashCode() {
        return getRequestedScopes().hashCode();
    }

    public String getUid() {
        return this.a;
    }

    public String getOpenId() {
        return this.b;
    }

    public String getDisplayName() {
        return this.c;
    }

    public Uri getPhotoUrl() {
        return Uri.parse(this.d == null ? "" : this.d);
    }

    public String getAccessToken() {
        return this.e;
    }

    public int getStatus() {
        return this.f;
    }

    public int getGender() {
        return this.g;
    }

    public Set<Scope> getGrantedScopes() {
        return this.j;
    }

    public String getServiceCountryCode() {
        return this.h;
    }

    public String getCountryCode() {
        return this.i;
    }

    public String getServerAuthCode() {
        return this.k;
    }

    public String getEmail() {
        return this.m;
    }

    public void setEmail(String str) {
        this.m = str;
    }

    public Account getAccount() {
        return this.m == null ? null : new Account(this.m, "com.huawei");
    }

    public Set<Scope> getExtensionScopes() {
        return this.n;
    }

    public void setExtensionScopes(Set<Scope> set) {
        this.n = set;
    }

    public String getIdToken() {
        return this.o;
    }

    public void setIdToken(String str) {
        this.o = str;
    }

    public long getExpirationTimeSecs() {
        return this.p;
    }

    public void setExpirationTimeSecs(long j) {
        this.p = j;
    }

    public String getGivenName() {
        return this.q;
    }

    public void setGivenName(String str) {
        this.q = str;
    }

    public String getFamilyName() {
        return this.r;
    }

    public void setFamilyName(String str) {
        this.r = str;
    }

    public String getId() {
        return getUnionId();
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{uid: ").append(this.a).append(',');
        stringBuilder.append("displayName: ").append(this.c).append(',');
        stringBuilder.append("photoUriString: ").append(this.d).append(',');
        stringBuilder.append("status: ").append(this.f).append(',');
        stringBuilder.append("gender: ").append(this.g).append(',');
        stringBuilder.append("serviceCountryCode: ").append(this.h).append(',');
        stringBuilder.append("countryCode: ").append(this.i).append('}');
        return stringBuilder.toString();
    }

    public String getUnionId() {
        return this.l;
    }

    public boolean isExpired() {
        if (this.p <= 300 || System.currentTimeMillis() / 1000 < this.p - 300) {
            return false;
        }
        return true;
    }

    public SignInHuaweiId requestExtraScopes(Scope... scopeArr) {
        if (scopeArr != null) {
            Collections.addAll(this.n, scopeArr);
        }
        return this;
    }

    public Set<Scope> getRequestedScopes() {
        Set<Scope> hashSet = new HashSet(this.j);
        hashSet.addAll(this.n);
        return hashSet;
    }

    public String getPhotoUriString() {
        return this.d;
    }

    public void setPhotoUriString(String str) {
        this.d = str;
    }
}
